package com.yuanqi.dz.dc.service.impl;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import org.apache.commons.lang3.ArrayUtils;
import pnbclient.helper.StringHelper;
import pnbclient.util.PnbLogFactory;
import pnblog.PnbLogging;
import com.yuanqi.dz.dc.adapters.DCReadFile;
import com.yuanqi.dz.dc.info.MainConfig;
import com.yuanqi.dz.dc.info.MerchantConfig;
import com.yuanqi.dz.dc.util.FtpUtil;
import com.yuanqi.dz.dc.util.TextValidateUtil;
import com.yuanqitech.advancedpower.powerdate.DateUtil;
import com.yuanqitech.advancedpower.powerdate.PowerDateFormat;
import com.yuanqitech.powermop.dz.DZDataInterface;
import com.yuanqitech.powermop.dz.DZInfo;
import com.yuanqitech.powermop.dz.DZInfoArray;

public class CMBCDZServiceImpl implements DZDataInterface{
	private static  String comm="common";

	public static final PnbLogging LOG = PnbLogFactory.getLogger("DZ_EXT_LOG");

	public static void main(String[] args) {
		DZInfoArray d = new CMBCDZServiceImpl().getDZData("305420180624900","20170329");
		if(null != d){
			DZInfo[] dz=d.getDzInfo();
			if(null!=dz && dz.length>0){
				for (int i = 0; i < dz.length; i++) 
					System.out.println(dz[i].toString());
				System.out.println(dz.length);
			}
		}
	}
	public DZInfoArray getDZData(String mercharnum, String dzDate) {
		DZInfoArray  dzInfoArray=new DZInfoArray();
		MerchantConfig  config2 =  TextValidateUtil.config.get("msdc").get(mercharnum);
		if(null == config2)
			config2 = TextValidateUtil.config.get("msdc").get(comm);
		MainConfig  main = config2.getMainConfig();
		String source_type = main.getSourceType();
		String address = main.getFtpIp();
		String s1_local_path = main.getS1LocalPath();
		String s3_local_path = main.getS3LocalPath();
		String s1_fileName = main.getS1FileName();
		String s3_fileName = main.getS3FileName();
		String dz_date_format = main.getDzDateFormat();
		String dz_date_addday = main.getDzDateAddday();
		int ftp_port = main.getFtpPort();
		String username = main.getFtpUsername();
		String password = main.getFtpPwd();
		String remotePath = main.getFtpRemote() ;

		SimpleDateFormat sdf = new SimpleDateFormat(dz_date_format);
		Calendar calendar = Calendar.getInstance();
		if (StringHelper.isEmpty(dzDate)) 
			calendar.add(Calendar.DAY_OF_MONTH, Integer.valueOf(dz_date_addday.toString()));
		else 
			calendar.setTime(PowerDateFormat.getInstance(DateUtil.YYYYMMDD).parse(dzDate));
		String s1_filename = s1_fileName.replace("{merchant}", mercharnum).replace("{date}", sdf.format(calendar.getTime()));
		String s3_filename = s3_fileName.replace("{merchant}", mercharnum).replace("{date}", sdf.format(calendar.getTime()));
		File s1_file = new File(s1_local_path, s1_filename);
		File s3_file = new File(s3_local_path, s3_filename);
		DZInfo[]  s1dzinfos = null;
		DZInfo[]  s3dzinfos = null;
		if(null != source_type && source_type.equals("1")){
			s1dzinfos=	new DCReadFile(dzDate).readFile2(s1_file, mercharnum, dzDate, config2,"S1");
			s3dzinfos = new DCReadFile(dzDate).readFile2(s3_file, mercharnum, dzDate, config2,"S3");
		}else if("2".equals(source_type)){
			boolean download = false;
			boolean download2 = false;
			int port = 0==ftp_port ? 21 : Integer.valueOf(ftp_port);
			try {
				download = new FtpUtil(address, port, username, password, remotePath).downFile(s1_file);
				download2 = new FtpUtil(address, port, username, password, remotePath).downFile(s3_file);
			} catch (Exception e) {
				dzInfoArray.setRc("-1");
				dzInfoArray.setErrormsg("ftp异常");
				return dzInfoArray;
			}
			if (!download ) 
				LOG.addError("ftp上未找到对账文件"+s1_file.getName());
			else{
				s1dzinfos = new DCReadFile(dzDate).readFile2(s1_file, mercharnum, dzDate, config2,"S1");
			}
			if(!download2)
				LOG.addError("ftp上未找到对账文件"+s3_file.getName());
			else{
				s3dzinfos = new DCReadFile(dzDate).readFile2(s3_file, mercharnum, dzDate, config2,"S3");
			}

		}else{
			dzInfoArray.setRc("-1");
			dzInfoArray.setErrormsg("未获取到实现方式获取实现方式暂未开放");
			return dzInfoArray;
		}
		LOG.addTrace("交易对账文件"+s1_file.getName()+"中获取到数据:"+s1dzinfos.length+"条");
		LOG.addTrace("退费对账文件"+s3_file.getName()+"中获取到数据:"+s3dzinfos.length+"条");
		DZInfo[] alldzinfos = ArrayUtils.addAll(s1dzinfos, s3dzinfos);
		LOG.addTrace("获取对账数据"+alldzinfos.length+"条");
		dzInfoArray.setDzInfo(alldzinfos);
		if(null == alldzinfos || alldzinfos.length==0){
			dzInfoArray.setRc("-1");
		}else{
			dzInfoArray.setRc("1");
		}
		return dzInfoArray;
	}

}
